package com.h.cheng.pselector.ui;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.h.cheng.pselector.R;
import com.h.cheng.pselector.base.BaseMediaLoader;
import com.h.cheng.pselector.base.load.IMediaLoader;
import com.h.cheng.pselector.base.load.PSConfig;
import com.h.cheng.pselector.bean.BaseMedia;
import com.h.cheng.pselector.view.MutipleTouchViewPager;
import com.h.cheng.pselector.view.phoneView.PhotoView;

import java.io.File;
import java.util.ArrayList;

public class ImgViewActivity extends AppCompatActivity {

    private RelativeLayout back;
    private TextView tv_simple_title;
    private MutipleTouchViewPager vp_view;
    private ArrayList<BaseMedia> list;

    private static final long MAX_IMAGE1 = 1024 * 1024L;
    private static final long MAX_IMAGE2 = 4 * MAX_IMAGE1;
    private int position;
    private PopupWindow pop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_img_view);
        initView();
        initData();
    }

    private void initData() {
        list = getIntent().getParcelableArrayListExtra(PSConfig.IMG_CONTENT);
        position = getIntent().getIntExtra(PSConfig.IMG_POSITION, 0);
        if (list != null) {
            tv_simple_title.setText((position + 1) + " / " + list.size());

            ImgAdapter adapter = new ImgAdapter();
            vp_view.setAdapter(adapter);
            if (position < list.size()) {
                vp_view.setCurrentItem(position);
            }

        }
    }

    private void initView() {
        back = findViewById(R.id.back);
        tv_simple_title = findViewById(R.id.tv_simple_title);
        vp_view = findViewById(R.id.vp_view);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        vp_view.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv_simple_title.setText((position + 1) + " / " + list.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class ImgAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, final int position) {

            PhotoView imageView = new PhotoView(ImgViewActivity.this);
            BaseMediaLoader.getInstance().displayIMGNocrop(imageView, list.get(position).getPath());
            container.addView(imageView);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

            imageView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    showTools(position, v);
                    return false;
                }
            });
            return imageView;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((ImageView) object);
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return object == view;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.activity_zoom_close);
    }


    private void showTools(int position, View parent) {
        View view = LayoutInflater.from(this).inflate(R.layout.pop_view_tools, null);
        initView(view, position);
        pop = new PopupWindow(this);
        pop.setContentView(view);
        pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        pop.setBackgroundDrawable(dw);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (pop != null && pop.isShowing()) {
                    pop.dismiss();
                }
                return false;
            }
        });
        pop.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

    }

    private void initView(View view, final int position) {
        View tv_save = view.findViewById(R.id.tv_save);
        View ll_cancle = view.findViewById(R.id.ll_cancle);
        ll_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pop != null && pop.isShowing()) {
                    pop.dismiss();
                }
            }
        });

        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pop != null && pop.isShowing()) {
                    pop.dismiss();
                }
                downImg(list.get(position).getPath());
            }
        });

    }

    private void downImg(String url) {
        BaseMediaLoader.getInstance().downImg(url, new IMediaLoader.FileCallBack() {
            @Override
            public void onSuccess(String path) {
                Toast.makeText(ImgViewActivity.this, "保存成功", Toast.LENGTH_LONG).show();

                Uri localUri = Uri.fromFile(new File(path));
                Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
                sendBroadcast(localIntent);

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ImgViewActivity.this, "保存失败", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onProgress(long totalSize, long downSize) {

            }
        });

    }

}
